package com.china.springbootmybatis.controller;

import com.china.springbootmybatis.commons.Rtn;
import com.china.springbootmybatis.dto.UserDto;
import com.china.springbootmybatis.mapper.UserMapper;
import com.china.springbootmybatis.model.User;
import com.china.springbootmybatis.util.LayuiUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author china@shanjun.wang
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @GetMapping(value = "getAllList")
    public Rtn getAllList() {
        return Rtn.success(userMapper.selectByPrimaryKey(1));
    }

    @GetMapping(value = "getPageList")
    public Rtn getPageList(UserDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return Rtn.success(new PageInfo<>(userMapper.selectByObject()));
    }

    /**
     * 返回 layui 数据表格特定格式
     * @param dto
     * @return
     */
    @ResponseBody
    @GetMapping(value = "getLayuiPageList")
    public String getLayuiPageList(UserDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        List<User> users = userMapper.selectByObject();
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        return LayuiUtil.table(userPageInfo.getTotal(), userPageInfo.getList());
    }
}
